function WindowControl() {

	this.controls = new List();
    this.activeWindow = null;
}
WindowControl.prototype = new MessUIElement();
WindowControl.prototype.toString = function() { return "js.mess.ui.controls.windows.WindowControl"; };
MessUIElement.prototype.addTagMapping("windowControl", "WindowControl");


    WindowControl.prototype.build = function() {

        // initialize control
        this.initControl();

        // build dom object
		this.buildDOMObject();
	};
	
	WindowControl.prototype.buildDOMObject = function() {
		
		var container = DIV();
            container.id = this.id;
			container.style.backgroundColor = "#FFFFFF";

        this.setDomObject(container, true, true);
	};
	
	WindowControl.prototype.addControl = function(control) {
	
		// add control
		if (control instanceof List) {
		// adding collection of child window controls
			for (var it = control.iterator(); it.hasNext();) {
				var oControl = it.next();
					oControl.shrink();
				this.controls.add(oControl);
			}
		}
		else {
		// adding single child window control
			this.controls.add(control);
		}
		
		return control;
	};
	
	/**
	* Return list of child controls
	*
	* @public
	* @method  getControls
	* @return  {List} collection of child controls or empty list
	*/
	WindowControl.prototype.getControls = function() {
	
		return this.controls;
	};
	
	WindowControl.prototype.buildChildControls = function() {
	
		for (var it = this.controls.iterator(); it.hasNext();) {

            // build child control & add to dom tree
            var control = this.buildChildControlObject(it.next());
            this.domObject.appendChild( control.render() );
		}

        // try building child controls
        for (var it = this.controls.iterator(); it.hasNext();) {
            var control = it.next();
            try {
                control.buildChildControls();
            }
            catch (ex) {}
        }

        // expand first child control
		if (!this.controls.isEmpty()) {
            this.activeWindow = this.controls.get(0);
            this.controls.get(0).expand();
        }
	};
	
	WindowControl.prototype.isHorizontal = function() {
	
		return this.parent.isHorizontal();
	};
	
	WindowControl.prototype.isVertical = function() {
	
		return this.parent.isVertical();
	};
	
	/**
	* Sets control width
	 *
	 * @public
	 * @method  setControlWidth
	 * @param   {Number} width  width to be set
	 */
    WindowControl.prototype.setControlWidth = function(width) {

		// set this window width
        this.controlWidth = width;
        try {
            this.setWidth(width);
        } catch(ex) {}

		// update inner windows widths
		for (var it = this.controls.iterator(); it.hasNext();) {
		
			var control = it.next();
				control.setControlWidth(width);
		}
    };

    /**
    * Sets control height
    *
    * @public
    * @method  setControlHeight
    * @param   {Number} height  height to be set
    */
    WindowControl.prototype.setControlHeight = function(height) {

        this.setHeight( height );
        // update height of expanded (active) Window
        if (this.activeWindow !== null) {
	        this.activeWindow.setControlHeight(this.getAvailableSpace() + this.activeWindow.getHeaderSize());
        }
    };
	
	/**
	* Replaces selected child control.
	*
	* @public
	* @method  replaceControl
	* @param  {MessUIElement} control  replacement control
	*/
	WindowControl.prototype.replaceControl = function(control) {
	
		// find child node with id matching control id
		var nodes = this.domObject.childNodes;
		
		for (var i = 0; i < nodes.length; i++) {
			var node = nodes[i];
			if (node.id === control.getId()) {
					
				// found node, replace current dom representation with new one
				this.domObject.replaceChild(control.render(), node);
				break;
			}
		}
    };

    /**
    * Return available space for extended window
    *
    * @private
    * @method  getAvailableSpace
    * @return  {Number} available space for control to expand into
    */
    WindowControl.prototype.getAvailableSpace = function() {

		var space = this.getHeight();
        if (!this.controls.isEmpty()) {
        	// sum up heights of window headers
        	for (var it = this.controls.iterator(); it.hasNext();) {
        		space -= it.next().getHeaderSize();
        	}
            return space;
        }
        return 0;
    };

    /**
    * Returns previous sibling for specified control
    *
    * @public
    * @method  getPrevSibling
    * @param   {Window} win   calling window
    * @return  {Object} previous sibling or null if none present
    */
    WindowControl.prototype.getPrevSibling = function(win) {

        var idx = this.controls.indexOf(win);
        return this.controls.get(--idx);
    };

    /**
    * Returns next sibling for specified control
    *
    * @public
    * @method  getNextSibling
    * @param   {Window} win   calling window
    * @return  {Object} next sibling or null if none present
    */
    WindowControl.prototype.getNextSibling = function(win) {

        var idx = this.controls.indexOf(win);
        return this.controls.get(++idx);
    };

    /**
    * Performs shrinking logic. Function called by window beeing shrinked.
    *
    * @public
    * @method  shrink
    * @param   {Window} calling window
    */
    WindowControl.prototype.shrink = function(win) {

        if (win === this.activeWindow) {
            this.activeWindow = null;
            win.buildDOMObjectShrinked();

            // find new active window and expand it
            var nextSibling = this.getNextSibling(win);
            var prevSibling = this.getPrevSibling(win);

			if (prevSibling !== null) {
				 this.activeWindow = prevSibling;
                prevSibling.expand();
			}
			else if (nextSibling !== null) {
                this.activeWindow = nextSibling;
                nextSibling.expand();
            }
            else {
                throw "error";
            }
        }
        else {
            win.buildDOMObjectShrinked();
        }
    };

    /**
    * Performs expanding logic. Function called by window beeing expanded.
    *
    * @public
    * @method  expand
    * @param   {Window} calling window
    */
    WindowControl.prototype.expand = function(win) {

        if (win !== this.activeWindow) {
            var oldActive = this.activeWindow;
            this.activeWindow = win;
            oldActive.shrink();

        }
        win.buildButtonsBox(true);
        win.setControlHeight(this.getAvailableSpace() + win.getHeaderSize());
    };
    
    /**
     * Return minimal height of wincontrol (window container)
     * 
     * @public
     * @method  getMinHeight
     * @return  {Number} min control height
     */
     WindowControl.prototype.getMinHeight = function() {
     	
		if (!this.controls.isEmpty()) {
			// sum up space occupied by window headers
			var min = 0;
			for (var it = this.controls.iterator(); it.hasNext();) {
				min += it.next().getHeaderSize();
			}
        }
        return 0;
     }




